<?php
if (empty($modelClass)) {
	$modelClass = Inflector::singularize($this->name);
}
if (!isset($className)) {
	$className = strtolower($this->name);
}
$what = isset($this->request->data[$modelClass]['id']) ? __d('croogo', 'Edit') : __d('croogo', 'Add');
?>
<h2 class="hidden-desktop">
<?php
	if ($titleBlock = $this->fetch('title')):
		echo $titleBlock;
	else:
		echo !empty($title_for_layout) ? $title_for_layout : $what . ' ' . $modelClass;
	endif;
?>
</h2>

<?php 
        if ($formsBlock = $this->fetch('forms')): 
                echo $formsBlock;
        else:
                echo $this->Form->create($modelClass);
        endif;
?>

<?php if ($actionsBlock = $this->fetch('actions')): ?>
<div class="row-fluid">
	<div class="span12 actions">
		<ul class="nav-buttons">
			<?php echo $actionsBlock; ?>
		</ul>
	</div>
</div>
<?php endif; ?>

<?php if ($contentBlock = $this->fetch('content')): ?>
	<?php echo $contentBlock; ?>
<?php else: ?>
	<?php
		$tabId = 'tabitem-' . Inflector::slug(strtolower($modelClass), '-');
		echo $this->Form->create($modelClass);
		if (isset($this->request->data[$modelClass]['id'])) {
			echo $this->Form->input('id');
		}
	?>
	<div class="row-fluid">
		<div class="span8">
			<ul class="nav nav-tabs">
			<?php
				echo $this->Croogo->adminTab(__d('croogo', $modelClass), "#$tabId");
				echo $this->Croogo->adminTabs();
			?>
			</ul>

			<?php
				$content = '';
                                        $status = '';
                                        $data_chooser = '';
                                        $data_chooser_field = '';
				foreach ($editFields as $field => $opts):
					if (is_string($opts)) {
						$field = $opts;
						$opts = array(
							'class' => 'span10',
							'label' => false,
							'tooltip' => ucfirst($field),
						);
					} else {
                                                          if(isset($opts['chooser'])){
                                                                  $data_chooser .='{ type: "'.$opts['chooser']['type'].'", target: \'#'.$modelClass.Inflector::classify($field).'\', attr: \''.$opts['chooser']['select'].'\' },';                                                                  
                                                                  if(isset($opts['chooser']['fields'])):
                                                                        $data_chooser .='{ type: "'.$opts['chooser']['type'].'", target: \'#'.$modelClass.Inflector::classify($opts['chooser']['fields']['field']).'\', attr: \''.$opts['chooser']['fields']['select'].'\' },';                                                                          
                                                                        $data_chooser_field = $this->Form->input($opts['chooser']['fields']['field'],array('type'=>'hidden'));
                                                                        $this->Form->unlockField($opts['chooser']['fields']['field']);
                                                                  endif;                                                                  
                                                                  
                                                                  $attrs = $opts;
                                                                  
                                                                  $opts = array(                                                                      
                                                                      'type'=>'text',
                                                                      'class'=>'span9',
                                                                      'readonly'=>'readonly',
                                                                      'after'=>' '.$this->Html->link(__d('croogo','Choose').' '.__d('croogo',$opts['chooser']['type']),
                                                                                        array(
                                                                                                  'plugin' => $opts['chooser']['url']['plugin'],
                                                                                                  'controller' => $opts['chooser']['url']['controller'],
                                                                                                  'action' => $opts['chooser']['url']['action'],
                                                                                                  '?' => array('chooser' => true,'KeepThis' => true,'TB_iframe' => true),
                                                                                        ),
                                                                                        array(
                                                                                                  'class' => 'action '.Inflector::camelize($opts['chooser']['type']).' chooser',
                                                                                        )
                                                                                   )
                                                                    );                                                                  
                                                                  
                                                                  unset($attrs['chooser']);
                                                                  $opts = Hash::merge($opts, $attrs);
                                                          }else{
                                                                $opts = Hash::merge(array('class' => 'span10'), $opts);
                                                                if((isset($opts['type']) && $opts['type']=='date') || (isset($opts['type']) && $opts['type']=='datetime')){
                                                                        $opts = Hash::merge(array('monthNames' => array('01' => __('January'),'02' => __('February'),'03' => __('March'),'04' => __('April'),'05' => __('May'),'06' => __('June'),'07' => __('July'),'08' => __('August'),'09' => __('September'),'10' => __('October'),'11' => __('November'),'12' => __('December'))), $opts);                                                                        
                                                                }
                                                          }
					}
                                                  if($field=='status'){
                                                        $opts['class'] = false;
                                                        $status = $this->Form->input($field, $opts);  
                                                  }else{
                                                        $content .= $this->Form->input($field, $opts);
                                                        if(isset($opts['slug'])){
                                                                $content .= $this->Layout->slug($modelClass.Inflector::camelize($opts['slug']));
                                                        }else if(!empty($data_chooser_field)){
                                                                $content .= $data_chooser_field;
                                                        }
                                                  }
				endforeach;
			?>

			<div class="tab-content">
			<?php
				if (!empty($content)):
					echo $this->Html->div('tab-pane', $content, array(
						'id' => $tabId,
					));
				endif;
				echo $this->Croogo->adminTabs();
			?>
			</div>
		</div>
		<div class="span4">
		<?php
			if ($buttonsBlock = $this->fetch('buttons')):
				$publishing = $buttonsBlock;
			else :
                                        echo $this->Html->beginBox(__d('croogo', 'Publishing')) .
                                        $this->Form->button(__d('croogo', 'Apply'), array('name' => 'apply')) .
                                        $this->Form->button(__d('croogo', 'Save'), array('class' => 'btn btn-primary')) .
                                        $this->Html->link(__d('croogo', 'Cancel'), array('action' => 'index'), array('class' => 'btn btn-danger')) .
                                        $status.
                                        $this->Html->endBox();

			endif;
			echo $this->Croogo->adminBoxes();
		?>
		</div>
	</div>
	<?php echo $this->Form->end(); ?>
<?php endif; ?>

<?php
if(isset($data_chooser) && !empty($data_chooser)):
$script =<<<EOF
$('.chooser').itemChooser({
	fields: [{$data_chooser}]
});
EOF;
$this->Js->buffer($script);
endif;
?>